//
//  CoGeUncompatibePluginController.m
//  CoGeOpenSource
//
//  Created by Tamas Nagy on 5/13/11.
//  Copyright 2011 home. All rights reserved.
//

#import "CoGeUncompatibePluginController.h"

#define MOVE @"/bin/mv"

@implementation CoGeUncompatibePluginController

@synthesize checkInDirs;
@synthesize fileManager;

- (id)init
{
    self = [super init];
    if (self) {
        NSFileManager *tempMan = [[NSFileManager alloc] init];
        NSArray *tempArray = [[NSArray alloc] initWithObjects:@"/Library/Graphics/Quartz Composer Patches", @"~/Library/Graphics/Quartz Composer Patches", @"/System/Library/Graphics/Quartz Composer Patches",@"/Library/Graphics/Quartz Composer Plug-Ins", @"/System/Library/Graphics/Quartz Composer Plug-Ins", @"~/Library/Graphics/Quartz Composer Plug-Ins",nil];
        
        self.fileManager = tempMan;
        self.checkInDirs = tempArray;
        
        [tempMan release];
        [tempArray release];
    }
    
    return self;
}

- (void)dealloc
{
    [fileManager release];
    [checkInDirs release];
    
    [super dealloc];
}

-(void)checkForUncompatibleQCPlugin:(NSString *)file {
    
    NSLog(@"checkForUncompatibleQCPlugin");
    
    [[file retain] autorelease];
    
    for(id dir in self.checkInDirs) {

        NSString *currentplugin = [[NSString stringWithFormat:@"%@/%@.plugin", dir,file] stringByExpandingTildeInPath];
        
       // NSLog(@"currentplugin: %@", currentplugin);
        
        if ([fileManager fileExistsAtPath:currentplugin]) {
            
            NSAlert *alert = [[NSAlert alloc] init];
            [alert addButtonWithTitle:@"Move the plugin"];
            [alert addButtonWithTitle:@"Cancel"];
            [alert setMessageText:@"Uncompatible plugin detected!"];
            [alert setInformativeText:[NSString stringWithFormat:@"CoGe just detected an uncompatible Quartz Composer plugin: %@.plugin. Uncompatible plugins may cause problems and produces crashes. Its highly recommended to deactivate the plugin. Plugin deactivation means the plugin will moved to your desktop to a folder named 'CoGe Uncompatible QCPlugins'.",file]];
            [alert setAlertStyle:NSWarningAlertStyle];
            
            if ([alert runModal] == NSAlertFirstButtonReturn) {
                NSLog(@"NSAlertFirstButtonReturn");
                // OK clicked, delete the record
                [self movePluginToDesktop:file fromFolder:dir];
            } else {
                
                NSLog(@" NO NSAlertFirstButtonReturn");
                
            }
            
            [alert release];
            
        }

    }
        
}

-(void)movePluginToDesktop:(NSString *)plugin fromFolder:(NSString *)folder {
    
    NSLog(@"movePluginToDesktop!");
    
    [plugin retain];
    [folder retain];
    
    if (![[BLAuthentication sharedInstance] isAuthenticated:MOVE]) {
        [[BLAuthentication sharedInstance] authenticate:MOVE];
    }

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
    NSString *desktopDir = [paths objectAtIndex:0];

    
    if (![fileManager fileExistsAtPath:[NSString stringWithFormat:@"%@/CoGe Uncompatible QCPlugins", desktopDir]]) {
        [fileManager createDirectoryAtPath:[NSString stringWithFormat:@"%@/CoGe Uncompatible QCPlugins", desktopDir] withIntermediateDirectories:NO attributes:nil error:nil];
    }

    NSString *source = [NSString stringWithFormat:@"%@/%@.plugin", folder, plugin];
    NSString *destination = [NSString stringWithFormat:@"%@/CoGe Uncompatible QCPlugins/%@.plugin", desktopDir,plugin];
    
    NSArray *arguments = [NSArray arrayWithObjects:source, destination, nil];
    [[BLAuthentication sharedInstance] executeCommand:MOVE withArgs:arguments];
      
    [plugin release];
    [folder release];
    
}


@end
